import random
import configs
from utils import service_functions

# from template.singleton import Singleton
from .numbers_decoder import Numbers
from .decoders import Decoders

obj_numbers = Numbers()


class Wrapper_decoders(Decoders):
    # def dec_activation_name(self) -> str:
    def set_control_mess(self, obj_control_messages) -> None:
        # super().__init__()
        self.obj_control_messages = obj_control_messages

    def set_work_mode(self, obj_work_mode):
        self.obj_work_mode = obj_work_mode

    def name_activation(self) -> str:
        if configs.name_separation:
            vosk_str_no_tags = service_functions.queue_voice_time_str_api_name(
                self.obj_work_mode
            )
            dec_name_lookup = self.search_name(vosk_str_no_tags)
            if dec_name_lookup:
                configs.speech_recognition_str_api_one = vosk_str_no_tags
                return vosk_str_no_tags
            else:
                return ""
        elif not configs.name_separation:
            self.obj_control_messages.turning_on_message("Скажите функцию")
            vosk_str_no_tags = service_functions.queue_voice_time_str_api()
            if vosk_str_no_tags:
                configs.speech_recognition_str_api_one = vosk_str_no_tags
                return vosk_str_no_tags
            else:
                return ""
        else:
            return ""

    # ---------------
    # Вызов функции self.dec_function_search
    def activation_func(self, name_or_func: str = "") -> list:
        if not name_or_func and not configs.name_separation:
            name_or_func = service_functions.queue_voice_str_api()
        elif name_or_func:
            pass
        func = self.search_cmd(name_or_func, "void")
        return func
        # Если слово в расп. речи совпало с импортированным декодером то вызываем self.dec_egg_decoders
        # return self.dec_egg_decoders(func, name_or_func)

    def selection_file_params(self, list_titles, voice_str):
        # Баг
        if configs.decoder_tags_vs_str == "tags":
            self.obj_control_messages.message("Скажите индекс")
            value = obj_numbers.number_activation_label([voice_str])[0]
            return value
        # Баг
        elif configs.decoder_tags_vs_str == "int":
            self.obj_control_messages.message("Скажите название")
            return self.decoder_titles(voice_str, list_titles)
        else:
            self.obj_control_messages.message("Скажите название")
            return self.decoder_titles(voice_str, list_titles)

    def selection_file_decoders(self, list_titles) -> int:
        flag_name_index = False
        for x in list_titles:
            for b in configs.dict_value_str_voice["dict_word"]:
                if b in x:
                    flag_name_index = True
        if flag_name_index:
            self.obj_control_messages.message("Скажите индекс")
            value = obj_numbers.one_int_index_number(
                [service_functions.queue_voice_str_api()]
            )
            return value
        elif configs.decoder_tags_vs_str == "int":
            self.obj_control_messages.message("Скажите индекс")
            value = obj_numbers.one_int_index_number(
                [service_functions.queue_voice_str_api()]
            )
            return value
        elif configs.decoder_tags_vs_str == "tags":
            self.obj_control_messages.message("Скажите название")
            return self.decoder_titles(
                service_functions.queue_voice_str_api(), list_titles
            )
        else:
            rand = random.randint(1, 2)
            if rand == 1:
                self.obj_control_messages.message("Скажите индекс")
                value = obj_numbers.one_int_index_number(
                    [service_functions.queue_voice_str_api()]
                )
                return value
            else:
                self.obj_control_messages.message("Скажите название")
                return self.decoder_titles(
                    service_functions.queue_voice_str_api(), list_titles
                )

    def decoder_rewind(self, list_cmd):
        # Обработка в цикле функции
        cmd_int = -1
        loop_make_flag = True
        cmd_make = ""
        while loop_make_flag:
            voice_str = service_functions.queue_voice_str_api()
            cmd_make = self.function_recognition(
                voice_str, "list_message_enter_dict_cmds"
            )
            if not cmd_make:
                cmd_int = self.selection_file_params(list_cmd, voice_str)
            elif cmd_int != -1 or cmd_make:
                loop_make_flag = False
                cmd_make = ""
                cmd_int = 0
        return cmd_int, cmd_make

    def decoder_rewind_and_index(self, json_name_key):
        # Обработка в цикле функции

        loop_make_flag = True
        cmd_make = []
        while loop_make_flag:
            voice_str = service_functions.queue_voice_str_api()
            cmd_make = self.function_recognition(voice_str, json_name_key)
            if cmd_make:
                cmd_make = cmd_make[0]
                loop_make_flag = False
        return cmd_make

    """
    def loop_func(self) -> dict:
        funcs_all = []
        menu_flag = ""
        funcs = []
        cmd = ""
        self.obj_control_messages.message("Циклическая обработка меток")
        while "end" not in menu_flag:
            str_cmd = service_functions.queue_voice_str_api()


            if len(str_cmd)!=0:
                cmd = self.dec_function_recognition(str_cmd, "loop_make")


            if (cmd in configs.cmds["loop_make"]):
                menu_flag = cmd

            if ("pause" not in menu_flag) and ("return_tags" not in menu_flag) and ("add" in menu_flag):

                funcs = self.dec_function_recognition_glav(str_cmd, "void")
                funcs_all = funcs_all +funcs["value"]

            elif ("return_tags" in menu_flag) == True:
                self.obj_control_messages.message(" ".join(funcs_all))

            elif ("clear" in menu_flag)==True:

                funcs_all.clear()
                self.obj_control_messages.message("Функции очищены")

            else:
                pass

        return funcs

    """

    def menu_dict_mess(self, mess_dict):
        exit_flag = False
        list_keys = list(mess_dict.keys())
        index = 0
        len_list_key = len(list_keys)
        while not exit_flag:
            if index >= 0 and index < len_list_key and not exit_flag:
                key = list_keys[index]
                cat_mess = mess_dict[key]
                self.obj_control_messages.message(f"Желаете прослушать {key}")
                permission_key = self.search_key_cmd(
                    service_functions.queue_voice_str_api(), "other"
                )
                if permission_key == "yes":
                    key = list_keys[index]
                    text_mess = cat_mess
                    self.obj_control_messages.message(text_mess)
                    index += 1
                elif permission_key == "back":
                    index -= 1
                    key = list_keys[index]
                    text_mess = cat_mess
                    self.obj_control_messages.message(text_mess)
                elif permission_key == "forward":
                    index += 1
                    key = list_keys[index]
                    text_mess = cat_mess
                    self.obj_control_messages.message(text_mess)
                elif permission_key == "metka_end":
                    exit_flag = True
                    break
                elif permission_key == "no":
                    exit_flag = True
                    break
                elif permission_key == "repeat":
                    pass
                else:
                    pass
            elif index < 0:
                index = 0
            elif index >= len_list_key:
                index = len_list_key - 1
        return ""

    def key_or_num(self, vois_str="", key_dict="other"):
        if not vois_str:
            vois_str = service_functions.queue_voice_str_api()
        num = -1
        permission_key = ""
        permission_key = self.search_key_cmd(vois_str, key_dict)
        list_number = obj_numbers.number_activation_label([vois_str])
        if list_number:
            num = list_number[0]
        return (permission_key, num)

    def cheek_say(self, message: str) -> bool:
        if configs.permission_check_speech:
            self.obj_control_messages.message(f"Вы имели виду '{message}'\n")
            cmd_str = service_functions.queue_voice_time_str_api()
            value = self.search_key_cmd(cmd_str, "other")

            if "yes" in value:
                return True
            elif "no" in value:
                return False
            else:
                return False
        else:
            return True
